﻿'Imports System.Text
'Imports Microsoft.Win32.TaskScheduler
'Imports ZViewTVDotNet.SQLiteWrapper

'Module gestionTache
'    Dim ts As New TaskService(Nothing, Nothing, Nothing, Nothing, True)
'    Dim user As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
'    Dim folder As TaskFolder = ts.RootFolder
'    Dim listeJob As TaskCollection = ts.RootFolder.Tasks
'    Dim currentTask As Task

'    Public Structure ZVTache
'        Public Nom As String
'        Public Qui As TypeEnregistrement
'        Public Id As String
'        Public debut As DateTime
'        Public duree As Integer
'        Public nomFichier As String
'        Public Quand As Repetion
'        Public Etat As Boolean
'    End Structure
'    Public Structure Listechaine
'        Public Nom As String
'        Public Id As String
'    End Structure
'    Public Enum TypeEnregistrement
'        ParZview
'        ParZguide
'    End Enum

'    <System.Flags()>
'    Public Enum Repetion
'        Aucun = 0
'        Lundi = 1
'        Mardi = 2
'        Mercredi = 4
'        Jeudi = 8
'        Vendredi = 16
'        Samedi = 32
'        Dimanche = 64
'    End Enum


'    Public Function RepetionToString(ByVal repet As Repetion) As String
'        Dim sbRetour As New StringBuilder
'        If (repet And Repetion.Aucun) <> 0 Then
'            sbRetour.Append("Aucun")
'        End If
'        If (repet And Repetion.Lundi) <> 0 Then
'            sbRetour.Append("Lu,")
'        End If
'        If (repet And Repetion.Mardi) <> 0 Then
'            sbRetour.Append("Ma,")
'        End If
'        If (repet And Repetion.Mercredi) <> 0 Then
'            sbRetour.Append("Me,")
'        End If
'        If (repet And Repetion.Jeudi) <> 0 Then
'            sbRetour.Append("Je,")
'        End If
'        If (repet And Repetion.Vendredi) <> 0 Then
'            sbRetour.Append("Ve,")
'        End If
'        If (repet And Repetion.Samedi) <> 0 Then
'            sbRetour.Append("Sa,")
'        End If
'        If (repet And Repetion.Dimanche) <> 0 Then
'            sbRetour.Append("Di,")
'        End If
'        If sbRetour.Length > 0 Then
'            sbRetour.Remove(sbRetour.Length - 1, 1)
'        End If
'        Return sbRetour.ToString

'    End Function

'    Public Function RepetionToBooleans(ByVal repet As Repetion) As Boolean()
'        Dim blnRetour(7) As Boolean

'        If (repet And Repetion.Lundi) <> 0 Then
'            blnRetour(0) = True
'        End If
'        If (repet And Repetion.Mardi) <> 0 Then
'            blnRetour(1) = True
'        End If
'        If (repet And Repetion.Mercredi) <> 0 Then
'            blnRetour(2) = True
'        End If
'        If (repet And Repetion.Jeudi) <> 0 Then
'            blnRetour(3) = True
'        End If
'        If (repet And Repetion.Vendredi) <> 0 Then
'            blnRetour(4) = True
'        End If
'        If (repet And Repetion.Samedi) <> 0 Then
'            blnRetour(5) = True
'        End If
'        If (repet And Repetion.Dimanche) <> 0 Then
'            blnRetour(6) = True
'        End If
'        Return blnRetour

'    End Function

'    Public Function BooleansToRepetion(ByVal lu As Boolean, ByVal ma As Boolean, ByVal mer As Boolean, ByVal je As Boolean, ByVal ve As Boolean, ByVal sa As Boolean, ByVal di As Boolean) As Repetion
'        Dim rnRetour As New Repetion
'        If lu Then
'            rnRetour = rnRetour Or Repetion.Lundi
'        End If
'        If ma Then
'            rnRetour = rnRetour Or Repetion.Mardi
'        End If
'        If mer Then
'            rnRetour = rnRetour Or Repetion.Mercredi
'        End If
'        If je Then
'            rnRetour = rnRetour Or Repetion.Jeudi
'        End If
'        If ve Then
'            rnRetour = rnRetour Or Repetion.Vendredi
'        End If
'        If sa Then
'            rnRetour = rnRetour Or Repetion.Samedi
'        End If
'        If di Then
'            rnRetour = rnRetour Or Repetion.Dimanche
'        End If
'        Return rnRetour
'    End Function

'    Public Function RepetetionToWeeklyTrigger(ByVal repet As Repetion) As WeeklyTrigger
'        Dim wtRetour As New WeeklyTrigger()
'        wtRetour.WeeksInterval = 1
'        wtRetour.DaysOfWeek = 0
'        If (repet And Repetion.Lundi) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Monday
'        End If
'        If (repet And Repetion.Mardi) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Tuesday
'        End If
'        If (repet And Repetion.Mercredi) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Wednesday
'        End If
'        If (repet And Repetion.Jeudi) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Thursday
'        End If
'        If (repet And Repetion.Vendredi) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Friday
'        End If
'        If (repet And Repetion.Samedi) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Saturday
'        End If
'        If (repet And Repetion.Dimanche) <> 0 Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Sunday
'        End If



'        Return wtRetour
'    End Function

'    Public Function BooleansToTrigger(ByVal lu As Boolean, ByVal ma As Boolean, ByVal mer As Boolean, ByVal je As Boolean, ByVal ve As Boolean, ByVal sa As Boolean, ByVal di As Boolean) As WeeklyTrigger
'        Dim wtRetour As New WeeklyTrigger
'        wtRetour.WeeksInterval = 1

'        If lu Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Monday
'        End If
'        If ma Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Tuesday
'        End If
'        If mer Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Wednesday
'        End If
'        If je Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Thursday
'        End If
'        If ve Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Friday
'        End If
'        If sa Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Saturday
'        End If
'        If di Then
'            wtRetour.DaysOfWeek = wtRetour.DaysOfWeek Or DaysOfTheWeek.Sunday
'        End If

'        Return wtRetour
'    End Function

'    Private Function ParseArguments() As String()
'        Dim titi As String = DirectCast(currentTask.Definition.Actions(0), ExecAction).Arguments
'        Dim toto() As String = titi.Split(CChar("/"))
'        Dim strRetour(3) As String
'        strRetour(0) = toto(1).Split(CChar(":"))(0) 'qui
'        strRetour(1) = toto(1).Split(CChar(":"))(1) 'id
'        'strRetour(2) = toto(1).Split(CChar(":"))(1) 'debut
'        strRetour(2) = toto(2).Split(CChar(":"))(1) 'duree
'        strRetour(3) = toto(3).Split(CChar(":"))(1) 'nomfichier
'        Return strRetour

'    End Function

'    Private Function TriggerToRepetion(ByVal trig As WeeklyTrigger) As Repetion
'        Dim rnRetour As Repetion
'        If (trig.DaysOfWeek And DaysOfTheWeek.Monday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Lundi
'        End If
'        If (trig.DaysOfWeek And DaysOfTheWeek.Tuesday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Mardi
'        End If
'        If (trig.DaysOfWeek And DaysOfTheWeek.Wednesday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Mercredi
'        End If
'        If (trig.DaysOfWeek And DaysOfTheWeek.Thursday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Jeudi
'        End If
'        If (trig.DaysOfWeek And DaysOfTheWeek.Friday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Vendredi
'        End If
'        If (trig.DaysOfWeek And DaysOfTheWeek.Saturday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Samedi
'        End If
'        If (trig.DaysOfWeek And DaysOfTheWeek.Sunday) <> 0 Then
'            rnRetour = rnRetour Or Repetion.Dimanche
'        End If
'        Return rnRetour
'    End Function

'    Public Function TaskToZVtache(ByVal nom As String) As ZVTache
'        currentTask = folder.Tasks(nom)

'        Dim Arg() As String = ParseArguments()

'        Dim zvRetour As New ZVTache

'        zvRetour.Nom = currentTask.Name                         'nom
'        zvRetour.Id = Arg(1)                                    'id
'        Select Case Arg(0)                                      'qui
'            Case "RecById"
'                zvRetour.Qui = TypeEnregistrement.ParZview
'            Case "RecByZGId"
'                zvRetour.Qui = TypeEnregistrement.ParZguide
'        End Select
'        'zvRetour.debut = DateTime.FromFileTime(CLng((Arg(2))))                'debut
'        zvRetour.debut = currentTask.Definition.Triggers(0).StartBoundary
'        zvRetour.duree = Integer.Parse(Arg(2))                  'duree
'        zvRetour.nomFichier = Arg(3)                            'nom fichier
'        zvRetour.Etat = currentTask.Definition.Settings.Enabled 'etat

'        If currentTask.Definition.Triggers.Count > 1 Then
'            zvRetour.Quand = TriggerToRepetion(CType(currentTask.Definition.Triggers(1), WeeklyTrigger))
'        Else
'            zvRetour.Quand = Repetion.Aucun
'        End If

'        Return zvRetour
'    End Function

'    Public Sub AddTask(ByVal z As ZVTache)
'        Dim NouvelleTache As TaskDefinition = ts.NewTask
'        With NouvelleTache
'            .Data = "donnees???"
'            .Principal.UserId = user
'            .Principal.LogonType = TaskLogonType.InteractiveToken

'            Select Case z.Qui
'                Case TypeEnregistrement.ParZguide
'                    .RegistrationInfo.Author = "ZGuideTV.Net"
'                Case TypeEnregistrement.ParZview
'                    .RegistrationInfo.Author = "ZviewTV.Net"
'            End Select

'            '.RegistrationInfo.Author = "ZviewTV.Net"
'            .RegistrationInfo.Description = "Enregistrement programmé de la télé"
'            .RegistrationInfo.Documentation = "Test planification de taches"
'            .Settings.DisallowStartIfOnBatteries = True
'            .Settings.Enabled = z.Etat                                                  ' z.enable
'            .Settings.ExecutionTimeLimit = TimeSpan.FromHours(24)
'            .Settings.Hidden = False
'            .Settings.IdleSettings.IdleDuration = TimeSpan.FromMinutes(20)
'            .Settings.IdleSettings.RestartOnIdle = False
'            .Settings.IdleSettings.StopOnIdleEnd = False
'            .Settings.IdleSettings.WaitTimeout = TimeSpan.FromMinutes(10)
'            .Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal
'            .Settings.RunOnlyIfIdle = False
'            .Settings.RunOnlyIfNetworkAvailable = False
'            .Settings.WakeToRun = True
'            .Settings.StopIfGoingOnBatteries = True
'        End With

'        Dim firstTrigger As New TimeTrigger
'        firstTrigger.StartBoundary = z.debut
'        NouvelleTache.Triggers.Add(firstTrigger)

'        If z.Quand <> Repetion.Aucun Then
'            Dim triggerRepetion As New WeeklyTrigger
'            triggerRepetion = RepetetionToWeeklyTrigger(z.Quand)                                  'z.quand
'            triggerRepetion.StartBoundary = z.debut                                         'z.debut
'            NouvelleTache.Triggers.Add(triggerRepetion)
'        End If

'        Dim argument As New StringBuilder
'        Select Case z.Qui                                                               'z.qui
'            Case TypeEnregistrement.ParZguide
'                argument.Append("/RecByZGId:")
'            Case TypeEnregistrement.ParZview
'                argument.Append("/RecById:")
'        End Select
'        argument.Append(z.Id)                                                           'z.id
'        argument.Append(" /Duration:")
'        argument.Append(z.duree)                                                        'z.duree
'        argument.Append(" /FileName:""")
'        argument.Append(z.nomFichier.Replace("""", ""))
'        argument.Append("""")


'        Dim Faire As New ExecAction(MePath, argument.ToString, Nothing)
'        NouvelleTache.Actions.Add(Faire)

'        Dim nomtache As String
'        If z.Nom.Length > 0 Then                                                        'z.nom
'            nomtache = z.Nom
'        Else
'            nomtache = "Enregistrement_" & z.debut.ToFileTime.ToString & "_" & z.Id & Rnd().ToString
'        End If
'        folder.RegisterTaskDefinition(z.Nom, NouvelleTache, TaskCreation.CreateOrUpdate, Nothing, Nothing, TaskLogonType.InteractiveToken, Nothing)


'    End Sub

'    Public Sub DeleteTask(ByVal nom As String)
'        folder.DeleteTask(currentTask.Name)
'    End Sub

'    Public Sub ModifyTask(ByVal z As ZVTache, ByVal oldname As String)
'        folder.DeleteTask(oldname)
'        AddTask(z)
'    End Sub

'    Public Function ListeZVTache() As ZVTache()
'        Dim TcRetour(listeJob.Count) As ZVTache
'        Dim cpt As Integer = 0
'        For Each tache As Task In listeJob
'            If tache.Definition.RegistrationInfo.Author = "ZviewTV.Net" Or tache.Definition.RegistrationInfo.Author = "ZGuideTV.Net" Then
'                TcRetour(cpt) = TaskToZVtache(tache.Name)
'                cpt += 1
'            End If
'        Next
'        If cpt > 0 Then
'            ReDim Preserve TcRetour(cpt - 1)
'        Else
'            ReDim TcRetour(-1)
'        End If

'        Return TcRetour
'    End Function


'    'A mettre dans database de zview
'    Public Function CreateListeChaineById() As Listechaine()
'        Dim lcRetour() As Listechaine
'        Dim db As SQLiteBase = New SQLiteBase
'        db.OpenDatabase(BDDPath & "Channels.db3")
'        Dim sql As String = "Select channel.Title || ' (' ||PlayList.Name || ')' ,channel.Id from channel, playlist where playlist.id = channel.ExtPlayListID"
'        Dim dtListeChaine As DataTable = db.ExecuteQuery(sql)
'        ReDim lcRetour(dtListeChaine.Rows.Count - 1)
'        For i As Integer = 0 To dtListeChaine.Rows.Count - 1
'            lcRetour(i).Nom = dtListeChaine.Rows(i).Item(0).ToString
'            lcRetour(i).Id = dtListeChaine.Rows(i).Item(1).ToString
'        Next
'        Return lcRetour
'    End Function
'    Public Function CreateListeChaineByZGId() As Listechaine()
'        Dim lcRetour() As Listechaine
'        Dim db As SQLiteBase = New SQLiteBase
'        db.OpenDatabase(BDDPath & "Channels.db3")
'        Dim sql As String = "Select channel.Title || ' (' ||PlayList.Name || ')',channel.ZGTVDotNETID from channel, playlist where playlist.id = channel.ExtPlayListID"
'        Dim dtListeChaine As DataTable = db.ExecuteQuery(sql)
'        ReDim lcRetour(dtListeChaine.Rows.Count - 1)
'        For i As Integer = 0 To dtListeChaine.Rows.Count - 1
'            lcRetour(i).Nom = dtListeChaine.Rows(i).Item(0).ToString
'            lcRetour(i).Id = dtListeChaine.Rows(i).Item(1).ToString
'        Next
'        Return lcRetour

'    End Function

'End Module
